﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.CommonDao;
using CSP.Dao.IObjectDaos.CSP;
using CSP.DataAccess;
using CSP.Model.CSP;
using CSP.Model.CSPEnum;
using System.Collections.ObjectModel;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class ServiceInfoDao : CSPCommonDao, IServiceInfoDao
    {
        public ServiceInfoDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        public ICollection<Model.CSP.ServiceInfo> FindAll()
        {
            return _context.ServiceInfoes.ToArray();
        }

        public ICollection<ServiceInfo> FindByUser(string userName, int pageIndex, int pageSize, out int pageCount)
        {
            pageCount = _context.ServiceInfoes.Where(s => s.username == userName).Count();
            var result = _context.ServiceInfoes.Where(s => s.username == userName && s.status != (byte)(int)ServiceInfoStatus.Snapshot).Skip(pageIndex * pageSize).Take(pageSize);
            if (result.Count() == 0)
            {
                return null;
            }
            else
            {
                return result.ToArray();
            }
        }

        /// <summary>
        /// 获取剩余服务库存量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetSurplusEnjoyCountById(int id)
        {
            return _context.ServiceInfoes.Where(s => s.id == id).Select(s => s.SurplusEnjoyCount).FirstOrDefault();
        }

        public Model.CSP.ServiceInfo GetDataById(int id)
        {
            return _context.ServiceInfoes.FirstOrDefault(a => a.id == id);
        }


        public ICollection<Model.CSP.ServiceInfo> GeDatatByIds(int[] Ids)
        {
            ICollection<Model.CSP.ServiceInfo> serviceInfoes = new Collection<Model.CSP.ServiceInfo>();
            var tmpServiceInfoes = _context.ServiceInfoes.Include(typeof(ServiceInfoServiceTypeRel).Name).Include(typeof(PriceUnit).Name)
                .Include(typeof(ServiceImage).Name).Include(typeof(ServiceInfoAddressRel).Name).Include(typeof(Address).Name).Where(a => Ids.Contains(a.id));
            if (tmpServiceInfoes.Count() != 0)
            {
                serviceInfoes = tmpServiceInfoes.ToArray();
            }
            return serviceInfoes;
        }

        public ServiceInfo GetByIdAndUser(int id, string userName)
        {
            return _context.ServiceInfoes.Include("ServiceImages").Include("ServiceInfoAddressRels").Include("ServiceInfoServiceTypeRels").FirstOrDefault(s => s.id == id && s.username == userName);
        }

        public void Add(Model.CSP.ServiceInfo obj)
        {
            _context.ServiceInfoes.Add(obj);
            _context.SaveChanges();
        }

        /// <summary>
        /// 创建一个ServiceInfo  并返回创建成功的实例
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ServiceInfo CreateServiceInfo(ServiceInfo obj)
        {
            ServiceInfo result = null;
            try
            {
                result = _context.ServiceInfoes.Add(obj);
                _context.SaveChanges();
            }
            catch (Exception ee)
            {
                throw ee.InnerException;
            }

            return result;
        }

        public void Update(Model.CSP.ServiceInfo obj)
        {
            ServiceInfo serviceInfo = GetDataById(obj.id);
            if (serviceInfo != null)
            {
                serviceInfo = obj;
            }
            _context.SaveChanges();
        }

        public ServiceInfo DeleteById(int id)
        {
            ServiceInfo serviceInfo = GetDataById(id);
            _context.ServiceInfoes.Remove(serviceInfo);
            _context.SaveChanges();

            return serviceInfo;
        }

        public ServiceInfo UpdateStatus(int id, ServiceInfoStatus serviceStatus)
        {
            ServiceInfo serviceInfo = GetDataById(id);
            if (serviceInfo != null)
            {
                serviceInfo.status = (byte)(int)serviceStatus;
                serviceInfo.OnLineTime = DateTime.Now;
            }
            _context.SaveChanges();

            return serviceInfo;
        }


        public List<int> FindIdsByUser(string userName)
        {
            List<int> result = new List<int>();
            var tmpResult = _context.ServiceInfoes.Where(s => s.username == userName && s.status != (int)ServiceInfoStatus.Snapshot).Select(s => s.id);
            if (tmpResult.Count() != 0)
            {
                result = tmpResult.ToList();
            }

            return result;
        }

        public ICollection<ServiceInfo> FindByIds(List<int> ids, int pageIndex, int pageSize)
        {
            ICollection<ServiceInfo> result = new Collection<ServiceInfo>();

            var tmpResult = _context.ServiceInfoes.Include("ServiceInfoAddressRels").Include("ServiceInfoServiceTypeRels").Where(s => ids.Contains(s.id)).OrderBy(s => s.LastModified).Skip(pageIndex * pageSize).Take(pageSize);
            if (tmpResult.Count() != 0)
            {
                result = tmpResult.ToArray();
            }

            return result;
        }


        public List<int> FindIdsByIdsAndStatus(List<int> ids, ServiceInfoStatus status)
        {
            List<int> result = new List<int>();
            var tmpResult = _context.ServiceInfoes.Where(s => ids.Contains(s.id) && s.status.Value == (byte)status).Select(s => s.id);
            if (tmpResult.Count() != 0)
            {
                result = tmpResult.ToList();
            }

            return result;
        }

        public List<int> FindByuserNameAndTitle(string userName, string title)
        {
            List<int> result = new List<int>();
            var tmpResult = _context.ServiceInfoes.Where(s => s.username == userName && s.title.Contains(title) && s.status != (byte)(int)ServiceInfoStatus.Snapshot).Select(s => s.id);
            if (tmpResult.Count() != 0)
            {
                result = tmpResult.ToList();
            }

            return result;
        }

        public ServiceInfo DeleteByIdAndUser(int id, string userName)
        {
            ServiceInfo serviceInfo = _context.ServiceInfoes.Include("ServiceImages").Include("ServiceInfoAddressRels").Include("ServiceInfoServiceTypeRels").FirstOrDefault(s => s.id == id && s.username == userName);
            if (serviceInfo != null)
            {
                _context.ServiceInfoes.Remove(serviceInfo);
            }
            _context.SaveChanges();

            return serviceInfo;
        }



        public int GetCountByUserAndStatus(string userName, ServiceInfoStatus status)
        {
            return _context.ServiceInfoes.Count(s=>s.username==userName&&s.status==(byte)(int)status);
        }
    }
}
